package com.boge.modules.ssgf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boge.common.utils.ShiroUtils;
import com.boge.modules.ssgf.entity.MaintenanceEntity;
import com.boge.modules.ssgf.service.MaintenanceService;
import com.boge.modules.sys.controller.WebController;
import com.boge.modules.sys.entity.SysUserEntity;
import com.boge.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boge.common.utils.PageUtils;
import com.boge.common.utils.Query;

import com.boge.modules.ssgf.dao.ReservationDao;
import com.boge.modules.ssgf.entity.ReservationEntity;
import com.boge.modules.ssgf.service.ReservationService;


@Service("reservationService")
public class ReservationServiceImpl extends ServiceImpl<ReservationDao, ReservationEntity> implements ReservationService {

    @Autowired
    private MaintenanceService maintenanceService;

    @Autowired
    private SysUserService userService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ReservationEntity> page = this.page(
                new Query<ReservationEntity>().getPage(params),
                new QueryWrapper<ReservationEntity>()
        );
        List<ReservationEntity> records = page.getRecords();
        if(records != null && records.size() > 0){
            for (ReservationEntity record : records) {
                Integer mId = record.getMaintenanceId();
                if(mId != null){
                    MaintenanceEntity maintenanceEntity = maintenanceService.getById(mId);
                    record.setMaName(maintenanceEntity.getTitle());
                    record.setImgUrl(WebController.BASE_URL+maintenanceEntity.getImg());
                }
                Integer userId = record.getUserId();
                SysUserEntity userEntity = userService.getById(userId);
                record.setUserName(userEntity.getUsername());

                Integer wxUser = record.getWxUser();
                if(wxUser != null){
                    SysUserEntity sysUserEntity = userService.getById(wxUser);
                    record.setWxUserName(sysUserEntity.getUsername());
                }
            }
        }

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPagePayOrder(Map<String, Object> params) {
        Long loginId = ShiroUtils.getUserId();
        IPage<ReservationEntity> page = this.page(
                new Query<ReservationEntity>().getPage(params),
                new LambdaQueryWrapper<ReservationEntity>().eq(ReservationEntity::getUserId,loginId)
                .eq(ReservationEntity::getStatus,3)
        );
        List<ReservationEntity> records = page.getRecords();
        if(records != null && records.size() > 0){
            for (ReservationEntity record : records) {
                Integer mId = record.getMaintenanceId();
                if(mId != null){
                    MaintenanceEntity maintenanceEntity = maintenanceService.getById(mId);
                    record.setMaName(maintenanceEntity.getTitle());
                    record.setImgUrl(WebController.BASE_URL+maintenanceEntity.getImg());
                }
                Integer userId = record.getUserId();
                SysUserEntity userEntity = userService.getById(userId);
                record.setUserName(userEntity.getUsername());

                Integer wxUser = record.getWxUser();
                if(wxUser != null){
                    SysUserEntity sysUserEntity = userService.getById(wxUser);
                    record.setWxUserName(sysUserEntity.getUsername());
                }
            }
        }

        return new PageUtils(page);
    }

}